package com.xbao.service.impl;

import com.xbao.core.http.CommRequest;
import com.xbao.dao.NoticeMapper;
import com.xbao.model.Notice;
import com.xbao.model.NoticeExample;
import com.xbao.model.req.NoticeReq;
import com.xbao.model.vo.NoticeVo;
import com.xbao.service.NoticeSerivce;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by qingshengzheng on 18/11/13.
 */
@Service
public class NoticeServiceImpl implements NoticeSerivce {
    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public void addNotice(NoticeReq req) {
        noticeMapper.insert(req);

    }

    @Override
    public void updateNotice(NoticeReq req) {
        noticeMapper.updateByPrimaryKey(req);

    }

    @Override
    public NoticeVo detailNotice(Long id) {
        Notice notice = noticeMapper.selectByPrimaryKey(id);
        NoticeVo vo = new NoticeVo();
        BeanUtils.copyProperties(notice, vo, NoticeVo.class);
        return vo;

    }

    @Override
    public void deleteNotice(Long id) {
        noticeMapper.deleteByPrimaryKey(id);


    }

    @Override
    public List<NoticeVo> queryNoticeList(CommRequest<NoticeReq> req) {
        NoticeReq notice = req.getData();
        NoticeExample example = new NoticeExample();
        NoticeExample.Criteria criteria = example.createCriteria().andSidEqualTo(notice.getSid());
        criteria.andCreatetimeBetween(notice.getReq_endtime(), notice.getReq_endtime());
        List<Notice> notices = noticeMapper.selectByExample(example);
        List<NoticeVo> vos = new ArrayList<NoticeVo>();
        for(Notice n:notices){
            NoticeVo nvo=new NoticeVo();
            BeanUtils.copyProperties(n,nvo,NoticeVo.class);
            vos.add(nvo);
        }
        return vos;
    }
}
